package com.hzhetun.example.service.serviceimpl;

import com.hzhetun.example.mapper.CrmCustomerArchivesMapper;
import com.hzhetun.example.mapper.CrmCustomerMapper;
import com.hzhetun.example.pojo.CrmCustomer;
import com.hzhetun.example.pojo.CrmCustomerArchives;
import com.hzhetun.example.service.CrmCustomerService;
import com.hzhetun.example.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenbingkun
 * @date 2018-12-23
 */
@Service
public class CrmCustomerServiceImpl implements CrmCustomerService{
    @Autowired
    private CrmCustomerMapper crmCustomerMapper;

    @Autowired
    private CrmCustomerArchivesMapper crmCustomerArchivesMapper;

    /**
     * 列表查询方法
     *
     * @param crmCustomer
     * @param page
     * @param limit
     * @return List<CrmCustomer>
     */
    @Override
    public List<CrmCustomer> findCrmCustomerList(CrmCustomer crmCustomer, int page, int limit) {
        Map<String,Object> map=new HashMap<>(16);
        map.put("customerName", StringUtil.isBlank(crmCustomer.getCustomerName())?null:"%"+crmCustomer.getCustomerName()+"%");
        map.put("city",StringUtil.isBlank(crmCustomer.getCity())?null:"%"+crmCustomer.getCity()+"%");
        map.put("offset",(page-1)*limit);
        map.put("rows",limit);
        return crmCustomerMapper.findCrmCustomerList(map);
    }

    /**
     * 记录总数查询方法
     *
     * @param crmCustomer
     * @return Long
     */
    @Override
    public Long count(CrmCustomer crmCustomer) {
        Map<String,Object> map=new HashMap<>(16);
        map.put("customerName", StringUtil.isBlank(crmCustomer.getCustomerName())?null:"%"+crmCustomer.getCustomerName()+"%");
        map.put("city",StringUtil.isBlank(crmCustomer.getCity())?null:"%"+crmCustomer.getCity()+"%");
//        map.put("offset",(page-1)*limit);
//        map.put("rows",limit);
        return crmCustomerMapper.count(map);
    }

    /**
     * 档案列表查询方法
     *
     * @param crmCustomerArchives
     * @param page
     * @param limit
     * @return List<CrmCustomerArchives>
     */
    @Override
    public List<CrmCustomerArchives> findCrmCustomerArchivesList(CrmCustomerArchives crmCustomerArchives, int page, int limit) {
        Map<String,Object> map=new HashMap<>(16);
        map.put("customerId", crmCustomerArchives.getCustomerId());
        map.put("offset",(page-1)*limit);
        map.put("rows",limit);
        return crmCustomerArchivesMapper.findCrmCustomerArchivesList(map);
    }

    /**
     * 记录总数查询方法
     *
     * @param crmCustomerArchives
     * @return Long
     */
    @Override
    public Long countCrmCustomerArchives(CrmCustomerArchives crmCustomerArchives) {
        Map<String,Object> map=new HashMap<>(16);
        map.put("customerId", crmCustomerArchives.getCustomerId());
        return crmCustomerArchivesMapper.count(map);
    }

    /**
     * 保存或者修改方法
     *
     * @param crmCustomer
     */
    @Override
    public void saveOrUpdate(CrmCustomer crmCustomer,String username) {
//        int userId=0;
        //新增
        if(crmCustomer.getId()==null){
            crmCustomer.setCreateTime(new Date());
            crmCustomer.setUpdateTime(new Date());
            crmCustomer.setCreateUser(username);
            crmCustomer.setUpdateUser(username);
            crmCustomerMapper.insertSelective(crmCustomer);
        }
        //修改
        else{

            crmCustomer.setUpdateTime(new Date());
            crmCustomer.setUpdateUser(username);
            crmCustomerMapper.updateByPrimaryKeySelective(crmCustomer);
        }
    }

    /**
     * 根据主键查询方法
     *
     * @param id
     * @return CrmCustomer
     */
    @Override
    public CrmCustomer findOne(int id) {
        return crmCustomerMapper.selectByPrimaryKey(id);
    }

    /**
     * 删除方法
     *
     * @param id
     * @return int
     */
    @Override
    public int del(int id) {
        return crmCustomerMapper.deleteByPrimaryKey(id);
    }

    /**
     * 查询所有的顾客
     *
     * @return List<CrmCustomer>
     */
    @Override
    public List<CrmCustomer> findAllCustomer() {
        return crmCustomerMapper.findAllCustomers();
    }
}
